package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.adpter.BeautufulAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.BeatifulAll;
import com.lanou.dllo.findfood.entity.FindLast;
import com.lanou.dllo.findfood.dialog.WebProgressDialog;
import com.lanou.dllo.findfood.util.MyFoodItemLisenter;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 美容减肥 发现页面
 * Created by dllo on 15/10/29.
 */
public class FindBeautifulActivity extends Activity implements MyFoodItemLisenter{
    private RecyclerView recyclerView;
    private RecyclerViewHeader viewHeader;
    private BeautufulAdpter adpter;
    private TextView newTitle;
    private ImageView goBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beautiful);
        initView();
        initData();
    }
    public void initView(){
        recyclerView = (RecyclerView) findViewById(R.id.bRecylerView);
        GridLayoutManager manager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(manager);
        adpter = new BeautufulAdpter();
        recyclerView.setAdapter(adpter);
        viewHeader = (RecyclerViewHeader) findViewById(R.id.Bheader);
        viewHeader.attachTo(recyclerView,true);
        adpter.setItemLisenter(this);
        newTitle = (TextView) findViewById(R.id.newTitle);
        newTitle.setText("美容减肥 吃出健康吃出好身材");
        goBack = (ImageView) findViewById(R.id.go);
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private  void initData(){
        final WebProgressDialog dialog = new WebProgressDialog(this,R.id.animaImgweb);
        dialog.show();
        String httpUrl = "http://apis.baidu.com/tngou/food/list";
        String httpArg = "id=79&page=1&rows=20";
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                BeatifulAll all = gson.fromJson(response, BeatifulAll.class);
                if (all != null) {
                    List<FindLast> result = all.getTngou();
                    adpter.addData(result);
                  dialog.dismiss();

                }
                //

            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                ;
            }
        }

        ) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);

    }


    @Override
    public void itemClickLisenter(FindLast last) {
        Intent intent = new Intent(this,BeautifulLastActivity.class);
        intent.putExtra("id",last.getId());
        startActivity(intent);
        overridePendingTransition(R.anim.in, R.anim.out);
    }
}
